import 'package:gate_cli/utils/intl_view/utils/project_intl.dart';
import 'package:gate_cli/utils/intl_view/utils/smartling_intl.dart';

class SubmitCommand {
  /// 比较当前项目中英文 arb 文件与关联 arb 文件的差异
  static Map<String, dynamic> difference({required List<String> associatedArbs}) {
    final associatedArbStrings = <String, String>{};
    for (var element in associatedArbs) {
      final name = element.replaceAll('.arb', '');
      final strings = SmartlingIntl.instance.getArb(name).strings;
      associatedArbStrings.addAll(strings);
    }
    final englishArb = ProjectIntl.instance.getArb('en');
    // 排除当前项目中已翻译但 smartling_flutter 中不存在的字符串【以前未使用 smartling_flutter 时翻译的文本】
    final turkishArb = ProjectIntl.instance.getArb('tr');
    return englishArb.differenceWithStrings(associatedArbStrings, skip: turkishArb.availableKeys);
  }

  static String submit(String targetArb, Map<String, String> strings) {
    /// 翻译名称
    final name = targetArb.replaceAll('.arb', '');

    /// 将需要提交的字符串保存到 smartling 的 arb 文件中
    SmartlingIntl.instance.getArb(name).saveAll(strings);

    /// 加载项目中的中文 arb 文件
    final zhStrings = ProjectIntl.instance.getArb('zh_Hans').strings;

    // 生成摘要
    String summary = '英文:\n';
    for (var element in strings.entries) {
      final key = element.key;
      final value = element.value;
      summary += '"$key": "${value.replaceAll('\n', '\\n')}"\n';
    }

    summary += '\n\n中文:\n';
    for (var element in strings.entries) {
      final key = element.key;
      final value = zhStrings[key] ?? '';
      summary += '"$key": "${value.replaceAll('\n', '\\n')}"\n';
    }

    return summary;
  }
}
